package kr.co.dcinside.agitater.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kr.co.dcinside.agitater.structure.Article;

public abstract class ArticleReader
{
    abstract public void refresh();

    public void read( final Article article ) throws IOException
    {
        final URL site = new URL( article.getArticleLink() );
        final BufferedReader in = new BufferedReader( new InputStreamReader(
            site.openStream(), "utf-8" ) );

        final StringBuilder sb = new StringBuilder();
        String inputLine, result;

        while ( ( inputLine = in.readLine() ) != null )
            sb.append( inputLine );
        in.close();

        result = sb.toString();

        // final Pattern writerPattern = Pattern
        // .compile( "<span\\b[^>]*title=\"([^>]*)\"\\b[^>]*>.*?</span>" );
        final Pattern replyPattern = Pattern
            .compile( "<td valign=top><span onClick=nameSearch\\(this\\) title=\"[^>^\"]*\"[^>]*>([^<]*)</span>[^<]*</td>[^<]*<td>[^<]*</td>[^<]*<td[^>]*>([^<]*)</td>[^<]*<td[^>]*><span[^>]*>([^<]*)" );
        final Matcher replyMatcher = replyPattern.matcher( result );

        while ( replyMatcher.find() )
        {
            // System.out.println( replyMatcher.group( 0 ) );
            System.out.print( replyMatcher.group( 1 ) );
            System.out.print( replyMatcher.group( 2 ) );
            System.out.print( replyMatcher.group( 3 ) );
            System.out.println();

        }

    }
}
